// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import '@/assets/style/element-ui/theme/index.css'
import store from './store/index'
import iview from 'iview'
import 'iview/dist/styles/iview.css'; 
import {default as api} from './utils/api'
import Service from './services'
import Message from 'iview/src/components/message'

require('./utils')
require('./mapping')
require('./mixin')
require('./filters')

Vue.use(ElementUI)
Vue.use(iview)
Vue.config.productionTip = false
Vue.prototype.api = api

console.log('current env ' + process.env.NODE_ENV + ' version 3')

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
    return
  }

  // 判断是否登录
  const isLogin = Service.User.isLogin()
  if (!isLogin) {
    if (process.env.NODE_ENV === 'production') {
      Service.User.toLogin()
    } else {
      next('/login')
    }
    return
  }

  // 判断页面是否路由权限
  const {meta = {}} = to
  let permissions = sessionStorage.getItem('permission') ? sessionStorage.getItem('permission').split(',') : []
  let user = sessionStorage.getItem('user')
  if (!user) {
    next('/login')
    return
  }
  if (meta.powerId && !permissions.includes(meta.powerId)) {
    next('/noauth')
    return
  }
  next()
})

function init () {
  /* eslint-disable no-new */
  new Vue({
    el: '#app',
    router,
    store,
    components: {App},
    template: '<App/>'
  })
}

if (process.env.NODE_ENV === 'production') {
  Service.User.getUserInfo().then(() => {
    init()
  })
} else {
  init()
}
